package system

import (
	"github.com/go-playground/validator/v10"
	"mycms/model/common/request"
)

// 菜单列表--分页条件查询及排序结构体
type SearchMenuParams struct {
	Type string `json:"type" form:"type"`
	request.PageInfo
}

// 查询菜单详情参数
type MenuInfoParams struct {
	Id uint `json:"id" form:"id" validate:"required,numeric" comment:"ID"`
}

//	更新菜单参数
type UpdateMenuParams struct {
	Id         uint   `json:"id" form:"id" validate:"required,numeric" comment:"ID"`
	Title      string `json:"title" form:"title" validate:"required" comment:"路由标题"`
	Name       string `json:"name" form:"name" validate:"required,alpha" comment:"路由名称"`
	ParentId   uint   `json:"parent_id" form:"parent_id" comment:"父菜单ID"`
	Path       string `json:"path" form:"path" validate:"required" comment:"路由path"`
	Status     uint   `json:"status" form:"status" comment:"状态"`
	Sort       int    `json:"sort" form:"sort" comment:"排序"`
	AlwaysShow uint   `json:"always_show" form:"always_show" comment:"是否显示"`
	Icon       string `json:"icon" form:"icon" comment:"菜单图标"`
	Component  string `json:"component" form:"component" comment:"Vue页面路径"`
	MenuType   uint   `json:"menu_type" form:"menu_type" comment:"菜单类型"`
	ApiId      []uint `json:"api_id" form:"api_id" validate:"CheckMenuApi"  errMsg:"API必选"`
}

// 新增菜单参数
type CreateMenuParams struct {
	Title      string `json:"title" form:"title" validate:"required" comment:"路由标题"`
	Name       string `json:"name" form:"name" validate:"required,alpha" comment:"路由名称（英文，前端使用）"`
	ParentId   uint   `json:"parent_id" form:"parent_id" comment:"父菜单ID"`
	Path       string `json:"path" form:"path" validate:"required" comment:"路由path"`
	Status     uint   `json:"status" form:"status" comment:"状态：0、禁用，1、正常"`
	Sort       int    `json:"sort" form:"sort" comment:"排序"`
	AlwaysShow uint   `json:"always_show" form:"always_show" comment:"是否显示：0、否，1、是"`
	Icon       string `json:"icon" form:"icon" comment:"菜单图标"`
	Component  string `json:"component" form:"component" comment:"Vue页面路径"`
	MenuType   uint   `json:"menu_type" form:"menu_type" comment:"菜单类型"`
	ApiId      []uint `json:"api_id" form:"api_id" validate:"CheckMenuApi"  errMsg:"API必选"`
}

// 删除菜单参数
type MenuDelParams struct {
	Id uint `json:"id" form:"id" validate:"required,numeric" comment:"ID"`
}



// 自定义校验方法 - Api参数验证
func CheckMenuApi(f validator.FieldLevel) bool {
	val := f.Parent()

	MenuType := val.FieldByName("MenuType")
	AlwaysShow := val.FieldByName("AlwaysShow")
	Api := val.FieldByName("ApiId")

	if (MenuType.Uint() == 2 || (MenuType.Uint() == 1 && AlwaysShow.Uint() == 0)) && Api.String() == "" {
		return false
	}
	return true
}

//	自定义校验方法 - Method参数验证
func CheckMenuMethod(f validator.FieldLevel) bool {
	val := f.Parent()

	MenuType := val.FieldByName("MenuType")
	AlwaysShow := val.FieldByName("AlwaysShow")
	Method := val.FieldByName("Method")

	if (MenuType.Uint() == 2 || (MenuType.Uint() == 1 && AlwaysShow.Uint() == 0)) && Method.String() == "" {
		return false
	}
	return true
}
